package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityDataHistory2;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbCapacityDataHistory2Dao {
    @Results(id = "pcbCapacityDataHistory2ResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcb_factory_id", property = "pcbFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_capacity_param_id", property = "pcbCapacityParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_param_value_type", property = "pcbParamValueType", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcb_param_key", property = "pcbParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcb_param_value", property = "pcbParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select *" +
            " from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY2 +
            "<where> " +
            "<if test='id != null'>" +
            " and id = #{id}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    PcbCapacityDataHistory2 getById(@Param("id") Integer id);

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select " +
            "id, version, pcb_capacity_param_id, pcb_factory_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort " +
            "from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY2 +
            "<where>" +
            "<if test='version != null '>" +
            "    and version = #{version}" +
            "</if>" +
            "<if test='pcbFactoryId != null '>" +
            "    and pcb_factory_id = #{pcbFactoryId}" +
            "</if>" +
            "<if test='pcbFactoryId == null '>" +
            "    and pcb_factory_id is null" +
            "</if>" +
            "<if test='pcbCapacityParamId != null '>" +
            "    and pcb_capacity_param_id = #{pcbCapacityParamId}" +
            "</if>" +
            "<if test='pcbParamValueType != null '>" +
            "    and pcb_param_value_type = #{pcbParamValueType}" +
            "</if>" +
            "<if test='pcbParamKey != null and pcbParamKey != \"\" '>" +
            "    and pcb_param_key = #{pcbParamKey}" +
            "</if>" +
            "<if test='pcbParamValue != null and pcbParamValue != \"\" '>" +
            "    and pcb_param_value = #{pcbParamValue}" +
            "</if>" +
            "<if test='sort != null'>" +
            "    and sort = #{sort}" +
            "</if>" +
            "<if test='status != null'>" +
            "    and status = #{status}" +
            "</if>" +
            "<if test='createTime != null'>" +
            "    and create_time = #{createTime}" +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    and update_time = #{updateTime}" +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    and create_user = #{createUser}" +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    and update_user = #{updateUser}" +
            "</if>" +
            "</where>" +
            " order by sort" +
            "</script>")

    @ResultMap(value = "pcbCapacityDataHistory2ResultMap")
    List<PcbCapacityDataHistory2> getAllByLimit(PcbCapacityDataHistory2 pcbCapacityDataHistory);

    /*
     *统计总行数
     */
    @Select("<script> " +
            "select count(1) " +
            "from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY2 +
            "</script>")
    Integer getAllCount();

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY2 + " (version, pcb_capacity_param_id, pcb_factory_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) values " +
            "(#{version}, #{pcbCapacityParamId}, #{pcbFactoryId}, #{pcbParamValueType}, #{pcbParamKey}, #{pcbParamValue}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(PcbCapacityDataHistory2 param);

    /*
     *批量新增
     */
    @Insert("<script>" +
            "insert into " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY2 +
            "(version, pcb_capacity_param_id, pcb_factory_id, pcb_param_value_type, pcb_param_key, pcb_param_value, create_time, create_user, update_time, update_user, status, sort) values " +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.version}, #{entity.pcbCapacityParamId}, #{entity.pcbFactoryId}, #{entity.pcbParamValueType}, #{entity.pcbParamKey}, #{entity.pcbParamValue}, #{entity.createTime}, #{entity.createUser}, #{entity.updateTime}, #{entity.updateUser}, #{entity.status}, #{entity.sort})" +
            "</foreach>" +
            "</script>")
    Integer insertBatch(@Param("entities") List<PcbCapacityDataHistory2> entities);

    /*
     *通过版本删除
     */
    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_DATA_HISTORY2 + " where version = #{version} ")
    Integer deleteByVersion(@Param("version") String version);

}
